<?php

/**
 * @Author: kendy
 * @Date:   2019-12-24 08:59:35
 * @Last Modified by:   jiayanting
 * @Last Modified time: 2020-04-03 13:40:33
 */
namespace app\hotel\model;

use think\Model;
use think\Db;
use think\Session;

class Settlements extends Model
{
	/**
	 * [getUnSettledList 获取未结算的订单]
	 * @Author   kendy
	 * @DateTime 2019-12-24T10:34:49+0800
	 * @param    [type]                   $where    [条件]
	 * @param    [type]                   $page     [当前页数]
	 * @param    [type]                   $startrow [每页显示条数]
	 * @param    [type]                   $iscount  [获取总条数]
	 * @return   [type]                             []
	 */
	public function getUnSettledList($where,$page, $pagesize, $iscount)
	{    
    	$where['is_del'] = 0;
    	$where['order_status'] = ['IN',[3,4]];
    	$where['settlement_id'] = 0;
    	$where['hotel_id'] = (int)Session::get('hotel_id');
    	$data =  Db::name('hotel_order')->where($where)->order('order_id', 'desc')
    	           ->limit($page,$pagesize)
		           ->field('order_id,total_price,order_sn,create_time,pay_type,order_price,procedure_fee')
                   ->select();
        if($iscount)
            return count($data);
        if(count($data)>0){
            foreach ($data as $key => $v) {
                $data[$key]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
                $data[$key]['pay_type'] = lvPayType($v['pay_type']);
            }
            return $data;
        }
        return $data;
	}


    /**
     * [getSettledList 获取已结算订单]
     * @Author   kendy
     * @DateTime 2019-12-24T11:00:59+0800
     * @return   [type]                   [description]
     */
	public function getSettledList($where,$page, $pagesize, $iscount)
	{ 
        $where = [];
        if(input('settlement_no')!='')
        	$where['settlement_no'] = ['like','%'.input('settlement_no').'%'];
        if(input('order_sn')!='')$where['order_sn'] = ['like','%'.input('order_sn').'%'];
        $where['settlement_status'] = 1;//(int)input('isFinish');
        $where['ho.is_del'] = 0;
        $where['order_status'] = ['IN',[3,4]];
        $where['ho.hotel_id'] = (int)Session::get('hotel_id');
    	$data = Db::name('hotel_order')
    	->alias('ho')
    	->join('__SETTLEMENTS__ s','ho.settlement_id=s.settlement_id')
    	->where($where)
    	->field('ho.order_id,ho.order_sn,ho.pay_type,total_price,order_price,ho.commission_fee,s.settlement_time,s.settlement_no')
    	->order('s.settlement_time desc')
        ->limit($page,$pagesize)
    	->select();
        if($iscount)
            return count($data);
       if(count($data)>0){
            foreach ($data as $key => $v) {
                $data[$key]['commission_fee'] = abs($v['commission_fee']);
                $data[$key]['pay_type'] = lvPayType($v['pay_type']);
                $data[$key]['settlement_time'] = date('Y-m-d H:i:s',$v['settlement_time']);
            }
        } 
        return $data;
	}


}